package LeetCode.leetcode144;

import java.util.ArrayList;
import java.util.List;

class Solution {

    /**
     * version 1
     *
     * @param root
     * @return
     */
    public List<Integer> preorderTraversal(TreeNode root) {

        return preOrder(new ArrayList<>(), root);
    }

    /**
     * version 1
     *
     * @param list
     * @param node
     * @return
     */
    public static List<Integer> preOrder(List<Integer> list, TreeNode node) {
        if (node == null)
            return list;
        //加入当前节点
        list.add(node.val);
        //左子树递归
        preOrder(list, node.left);
        //右子树递归
        preOrder(list, node.right);
        return list;
    }



    ////////
    ////////

    /**
     * version 2
     *
     * @param root
     * @return
     */
    public List<Integer> preorderTraversal2(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        preOrder2(list, root);
        return list;
    }

    /**
     * version 2
     *
     * @param list
     * @param node
     * @return
     */
    public static void preOrder2(List<Integer> list, TreeNode node) {
        if (node == null)
            return;
        //加入当前节点
        list.add(node.val);
        //左子树递归
        preOrder(list, node.left);
        //右子树递归
        preOrder(list, node.right);
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
